﻿using FinancialBook.Core.Service;
using FinancialBook.Models;
using MvvmCross.Core.ViewModels;
using MvvmCross.Platform;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Windows.Input;

namespace FinancialBook.Core.ViewModels
{
    public class TransactionNewViewModel : MvxViewModel
    {
        public ObservableCollection<Category> ListCategory { get; set; }
        ITransactionRepository contextT;
        ICategoryRepository contextC;

        #region Properties

        private string _transactionType;
        public string TransactionType
        {
            get { return _transactionType; }
            set
            {
                _transactionType = value;
                RaisePropertyChanged(() => TransactionType);
            }
        }

        private int _amount;
        public int Amount
        {
            get { return _amount; }
            set
            {
                _amount = value;
                RaisePropertyChanged(() => Amount);
            }
        }

        private string _category;
        public string Category
        {
            get { return _category; }
            set
            {
                _category = value;
                RaisePropertyChanged(() => Category);
            }
        }

        private string _description;
        public string Description
        {
            get { return _description; }
            set
            {
                _description = value;
                RaisePropertyChanged(() => Description);
            }
        }

        private string _date;
        public string Date
        {
            get { return _date; }
            set
            {
                _date = value;
                RaisePropertyChanged(() => Date);
            }
        }

        #endregion

        public TransactionNewViewModel()
        {
            ListCategory = new ObservableCollection<Category>();
            contextT = Mvx.Resolve<ITransactionRepository>();
            contextC = Mvx.Resolve<ICategoryRepository>();
            GetCategories();
        }

        public void GetCategories()
        {
            ListCategory.Clear();
            var cats = contextC.AllItems();
            foreach (var item in cats)
            {
                ListCategory.Add(item);
            }
        }

        public ICommand AddNewTransaction
        {
            get
            {
                return new MvxCommand(() =>
                {
                    var transaction = new Transaction();
                    transaction.Amount = Amount;
                    transaction.Category = Category;
                    transaction.Date = DateTime.Now.ToString();
                    transaction.Desc = Description;
                    transaction.Type = TransactionType;
                    contextT.Add(transaction);
                    ShowViewModel<TransactionViewModel>();
                });
            }
        }
    }
}